﻿using _1142063_1142080_1142098.Enum;
using _1142063_1142080_1142098.Models;
using _1142063_1142080_1142098.Models.DTO;
using _1142063_1142080_1142098.Models.Services;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _1142063_1142080_1142098.Controllers
{
    public class TourController : Controller
    {
        //
        // GET: /Tour/

        public ActionResult ChuongTrinhTour()
        {
            var chuongTrinhTourModel = new ChuongTrinhTourModels()
            {
                NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode()),
                NoiDenList = new List<NoiDen>(),
                GiaList = new List<DanhMuc>()
            };
            var idTour = new DateTime(long.Parse(Request["IdTour"]));
            var isTrongNuoc = byte.Parse(Request["IsTrongNuoc"]);
            if (isTrongNuoc == (byte)0)
            {
                var data = TourService.LayChuongTrinhTourNuocNgoai(idTour);
                chuongTrinhTourModel.ChuongTrinhTourList = data.Item1;
                chuongTrinhTourModel.Tour = data.Item2;
            }
            else
            {
                var data = TourService.LayChuongTrinhTourTrongNuoc(idTour);
                chuongTrinhTourModel.ChuongTrinhTourList = data.Item1;
                chuongTrinhTourModel.Tour = data.Item2;
            }
            return View(chuongTrinhTourModel);
        }

        public ActionResult ChiTietTourTrongNuoc()
        {
            var chiTietTourTrongNuocModel = new ChiTietTourTrongNuocModels()
            {
                NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode()),
                NoiDenList = new List<NoiDen>(),
                GiaList = new List<DanhMuc>()
            };
            var idTour = new DateTime(long.Parse(Request["IdTour"]));
            chiTietTourTrongNuocModel.ChiTietTour = TourService.LayChiTietTourTrongNuoc(idTour);
            return View(chiTietTourTrongNuocModel);
        }

        public ActionResult ChiTietTourNuocNgoai()
        {
            var chiTietTourNuocNgoaiModel = new ChiTietTourNuocNgoaiModels()
            {
                NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode()),
                NoiDenList = new List<NoiDen>(),
                GiaList = new List<DanhMuc>()
            };
            var idTour = new DateTime(long.Parse(Request["IdTour"]));
            chiTietTourNuocNgoaiModel.ChiTietTour = TourService.LayChiTietTourNuocNgoai(idTour);
            return View(chiTietTourNuocNgoaiModel);
        }

        public ActionResult DangKyTourTrongNuoc()
        {
            var dangKyTourTrongNuocModel = new DangKyTourTrongNuocModels()
            {
                NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode()),
                NoiDenList = new List<NoiDen>(),
                GiaList = new List<DanhMuc>()
            };
            var idTour = new DateTime(long.Parse(Request["IdTour"]));
            dangKyTourTrongNuocModel.ChiTietTour = TourService.LayTourTrongNuoc(idTour);
            dangKyTourTrongNuocModel.LoaiKhachList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.LoaiKhach.GetHashCode());
            return View(dangKyTourTrongNuocModel);
        }

        [HttpPost]
        public ActionResult DangKyTourTrongNuoc(DangKyTourTrongNuocModels dangKyTourTrongNuocModel)
        {
            dangKyTourTrongNuocModel.NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode());
            var idTour = new DateTime(long.Parse(Request["IdTour"]));
            dangKyTourTrongNuocModel.ChiTietTour = TourService.LayTourTrongNuoc(idTour);
            dangKyTourTrongNuocModel.LoaiKhachList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.LoaiKhach.GetHashCode());
            if(ModelState.IsValid)
            {
                dangKyTourTrongNuocModel.PhieuDangKyTour.IdTour = idTour;
                dangKyTourTrongNuocModel.PhieuDangKyTour.Id = DateTime.Now;
                dangKyTourTrongNuocModel.PhieuDangKyTour.MaPhieu = Guid.NewGuid();
                dangKyTourTrongNuocModel.ChiTietPhieuDangKyTourList = new List<ChiTietPhieuDangKyTour>();
                for (int i = 1; i <= dangKyTourTrongNuocModel.PhieuDangKyTour.TongSoKhach;i++)
                {
                    var chiTietPhieuDangKyTour = new ChiTietPhieuDangKyTour();
                    chiTietPhieuDangKyTour.DiaChi = Request["DiaChi"+i.ToString()];
                    chiTietPhieuDangKyTour.DoTuoi = byte.Parse(Request["DoTuoi" + i.ToString()]);
                    chiTietPhieuDangKyTour.Gia = decimal.Parse(Request["Gia" + i.ToString()]);
                    chiTietPhieuDangKyTour.NgaySinh = !string.IsNullOrEmpty(Request["NgaySinh" + i.ToString()]) ? DateTime.Parse(Request["NgaySinh" + i.ToString()]):(DateTime?)null;
                    chiTietPhieuDangKyTour.GioiTinh = byte.Parse(Request["GioiTinh" + i.ToString()]);
                    chiTietPhieuDangKyTour.HoTenKhachHang = Request["HoTen" + i.ToString()];
                    chiTietPhieuDangKyTour.Id = DateTime.Now;
                    chiTietPhieuDangKyTour.IdPhieuDangKyTour = dangKyTourTrongNuocModel.PhieuDangKyTour.Id;
                    chiTietPhieuDangKyTour.LoaiKhach = byte.Parse(Request["LoaiKhach" + i.ToString()]);
                    dangKyTourTrongNuocModel.ChiTietPhieuDangKyTourList.Add(chiTietPhieuDangKyTour);
                }
                dangKyTourTrongNuocModel.PhieuDangKyTour.TongTien = dangKyTourTrongNuocModel.ChiTietPhieuDangKyTourList.Sum(r => r.Gia);
                dangKyTourTrongNuocModel.Error = PhieuDangKyTourService.DangKyTour(dangKyTourTrongNuocModel.PhieuDangKyTour, dangKyTourTrongNuocModel.ChiTietPhieuDangKyTourList, (byte)1);
                if (string.IsNullOrEmpty(dangKyTourTrongNuocModel.Error))
                {
                    dangKyTourTrongNuocModel.Error = "Đăng ký thành công, số Booking của bạn là "+dangKyTourTrongNuocModel.PhieuDangKyTour.MaPhieu;
                }
                dangKyTourTrongNuocModel.PhieuDangKyTour.SoNguoiLon = 0;
                dangKyTourTrongNuocModel.PhieuDangKyTour.SoTreEm = 0;
                dangKyTourTrongNuocModel.PhieuDangKyTour.SoTreNho = 0;
                dangKyTourTrongNuocModel.PhieuDangKyTour.TongSoKhach = 0;
            }
            return View(dangKyTourTrongNuocModel);
        }

        public ActionResult DangKyTourNuocNgoai()
        {
            var dangKyTourNuocNgoaiModel = new DangKyTourNuocNgoaiModels()
            {
                NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode()),
                NoiDenList = new List<NoiDen>(),
                GiaList = new List<DanhMuc>()
            };
            var idTour = new DateTime(long.Parse(Request["IdTour"]));
            dangKyTourNuocNgoaiModel.ChiTietTour = TourService.LayTourNuocNgoai(idTour);
            dangKyTourNuocNgoaiModel.QuocTichList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.QuocTich.GetHashCode());
            dangKyTourNuocNgoaiModel.LoaiKhachList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.LoaiKhach.GetHashCode());
            return View(dangKyTourNuocNgoaiModel);
        }

        public ActionResult TraCuuBookingTrongNuoc()
        {
            var traCuuBookingModel = new TraCuuBookingTrongNuocModels()
            {
                NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode()),
                NoiDenList = new List<NoiDen>(),
                GiaList = new List<DanhMuc>()
            };
            return View(traCuuBookingModel);
        }

        [HttpPost]
        public ActionResult TraCuuBookingTrongNuoc(TraCuuBookingTrongNuocModels tracuuBookingModel)
        {
            tracuuBookingModel.NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode());
            tracuuBookingModel.NoiDenList = new List<NoiDen>();
            tracuuBookingModel.GiaList = new List<DanhMuc>();
            Guid temp;
            if (Guid.TryParse(tracuuBookingModel.MaBooking, out temp))
            {
                tracuuBookingModel.PhieuDangKy = TourService.LayPhieuDangKyByMaPhieu(new Guid(tracuuBookingModel.MaBooking), true);
                if (tracuuBookingModel.PhieuDangKy == null)
                {
                    tracuuBookingModel.Error = "Không tìm thấy";
                }
                else
                {
                    tracuuBookingModel.ChiTietTour = TourService.LayChiTietTourTrongNuoc(tracuuBookingModel.PhieuDangKy.IdTour);
                    tracuuBookingModel.ChiTietPhieuDangKyTourList = TourService.LayChiTietPhieuDangKyByIdPhieuDangKy(tracuuBookingModel.PhieuDangKy.Id,true);
                    tracuuBookingModel.LoaiKhachList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.LoaiKhach.GetHashCode());
                    tracuuBookingModel.DoTuoiList =
                        DanhMucService.LayDanhSach((byte) LoaiDanhMucEnum.DoTuoi.GetHashCode());
                }
            }
            else
            {
                tracuuBookingModel.Error = "Mã booking không hợp lệ";
            }
            return View(tracuuBookingModel);
        }
    }
}
